// from custom.js
jQuery(document).ready(function() {
    jQuery(function() {
        jQuery('ul.da-thumbs > li').hoverdir();
    });
});

jQuery(document).ready(function($) {

    $("a[data-pretty^='prettyPhoto']").prettyPhoto();

    $(".gallery:first a[data-pretty^='prettyPhoto']").prettyPhoto({animation_speed: 'normal', theme: 'pp_default', slideshow: 3000, autoplay_slideshow: false});
    $(".gallery:gt(0) a[data-pretty^='prettyPhoto']").prettyPhoto({animation_speed: 'fast', slideshow: 10000, hideflash: true});

    $("#custom_content a[data-pretty^='prettyPhoto']:first").prettyPhoto({
        custom_markup: '<div id="map_canvas" style="width:260px; height:265px"></div>',
        changepicturecallback: function() {
            initialize();
        }
    });

    $("#custom_content a[data-pretty^='prettyPhoto']:last").prettyPhoto({
        custom_markup: '<div id="bsap_1259344" class="bsarocks bsap_d49a0984d0f377271ccbf01a33f2b6d6"></div><div id="bsap_1237859" class="bsarocks bsap_d49a0984d0f377271ccbf01a33f2b6d6" style="height:260px"></div><div id="bsap_1251710" class="bsarocks bsap_d49a0984d0f377271ccbf01a33f2b6d6"></div>',
        changepicturecallback: function() {
            _bsap.exec();
        }
    });

    $('ul.nav li.dropdown').hover(function() {
        $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn();
    }, function() {
        $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut();
    });

    // tooltip
    $("a[data-rel^='tooltip']").tooltip();
    $('.tooltip').tooltip();

    $('#mainslider').flexslider({
        animation: "slide",
        controlNav: "thumbnails"
    });

});

var domain_url = "http://www.yyste.com/";

//function LoadJS( file_ ){
//    //should add domain here, or admin pages can not work
//    document.write( "<script type=\"text/javascript\" src=\""+domain_url+"static/js/" + file_ +   "\"><\/script>" );
//}
//
//LoadJS( "lib/jquery-1.7.min.js" );

$(document).ready(function(){
//    $('.x-tooltip').tooltip({placement:'bottom'});
    
    //set advertise when page load done
    $(".moviecover").each(
        function(index){
            var title = $(this).attr('alt');
            var imgItem = $(this);

            var OrgImgUrl = $(this).attr('src');
            var itemId = $(this).attr('itemid');
            
            // only when imgurl is default img, query douban API
            if( OrgImgUrl.indexOf('images/default.jpg') != -1 ){
                $.getJSON("https://api.douban.com/v2/movie/search?callback=?",
                {
                    start : 0,
                    count : 1,
                    q : title
                },
                function(data) {
                    if( typeof(data) != 'undefined' && data.count >= 1 ){
                        var doubanImgUrl = '';
                        if( typeof data.subjects[0] != 'undefined' ){
                        	doubanImgUrl = data.subjects[0].images.small;
                        }
                        imgItem.attr('src', doubanImgUrl ).load(
                            function() {
                                if((typeof this.naturalWidth != "undefined" && this.naturalWidth == 0 ) || this.readyState == 'uninitialized' ){
                                    this.attr('src', domain_url+'static/images/default.jpg' );
                                }
                                else{
                                    // update item image url when api call succeed
                                    $.getJSON( domain_url+"index.php?callback=?", 
                                    {
                                        action : 'updateImgUrl',
                                        id : itemId,
                                        imgurl : doubanImgUrl
                                    },
                                    function(data) {
                                        // alert( JSON.stringify(data) );
                                        // we don't care about the result
                                    });
                                }
                        });
                    }
                    else{
                        imgItem.attr('src', domain_url+'static/images/default.jpg' );
                    }
                });
            }
            
        }
    );

    if( typeof WeixinJSBridge != 'undefined' ){
        $('#weixinShare').show();
    }
    
    
    // search suggestion
    var last_search = '';
    $('#search_input').typeahead({
        source: function (typeahead, query) {
            if(last_search == typeahead)
                return;
            last_search = typeahead;
            return $.getJSON('index.php?action=search_typeahead&q=' + encodeURIComponent(typeahead),
                function (json) {
                    if(json.result == 0)
                        return query(json.data);
                }
            );
        }
    });
    
    $('#search_input_large').typeahead({
        source: function (typeahead, query) {
            if(last_search == typeahead)
                return;
            last_search = typeahead;
            return $.getJSON('index.php?action=search_typeahead&q=' + encodeURIComponent(typeahead),
                function (json) {
                    if(json.result == 0)
                        return query(json.data);
                }
            );
        }
    });
    
    $("img").lazyload({
//        placeholder: domain_url+'static/images/grep.gif',
//        effect: "fadeIn"
    });//延迟那些图片，可以自己定义

});


// 分享到朋友圈
function weixinShareTimeline(){
	alert("here");
	/*WeixinJSBridge.invoke('shareTimeline',{
		"img_url":"http://www.wmi.me/Content/images/apple-touch-icon.png",
		//"img_width":"640",
		//"img_height":"640",
		"link":"http://yyste.com",
		"desc": "人人P2P - 最新美剧、电影下载",
		"title":"人人P2P"
	});*/
	WeixinJSBridge.invoke('shareTimeline', { "title": "玩媒", "link": "http://www.wmi.me", "desc": "玩媒采用专业算法挖掘自媒体平台中的非结构化内容，根据传播力和可信度等相关因素，为财经用户提供精彩信息引爆点和决策数据价值点。玩转自媒体，策动人际圈影响力！", "img_url": "http://www.wmi.me/Content/images/apple-touch-icon.png" }); 
}

